package com.lfy.modules.safety.controller;

import com.lfy.modules.blog.domain.group.RemoveGroup;
import com.lfy.modules.blog.domain.group.SaveGroup;
import com.lfy.modules.blog.domain.group.UpdateGroup;
import com.lfy.modules.common.anno.Authentication;
import com.lfy.modules.common.anno.NoNeedLogin;
import com.lfy.modules.common.custom.constants.PageConstant;
import com.lfy.modules.common.domain.BaseEnum;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.safety.blankMenu.BlackMenuCache;
import com.lfy.modules.safety.domain.BlackList;
import com.lfy.modules.safety.domain.BlogPvStatistics;
import com.lfy.modules.safety.domain.BlogUvStatistics;
import com.lfy.modules.safety.domain.dto.BlackDTO;
import com.lfy.modules.safety.domain.vo.BlackListVO;
import com.lfy.modules.safety.service.BlackListService;
import com.lfy.modules.safety.statistics.pv.PvCache;
import com.lfy.modules.safety.statistics.uv.UvCache;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.TreeMap;

/**
 *  博客数据统计接口
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/28 14:36
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Resource
    private BlackListService blackListService;
    @GetMapping("/pv")
    @Authentication("sys:statistics:pv:get")
    public R<List<BlogPvStatistics>> getStatisticsPv() {
        return R.success(PvCache.getSevenDaysPv());
    }
    @GetMapping("/uv")
    @Authentication("sys:statistics:uv:get")
    public R<List<BlogUvStatistics>> getStatisticsUv() {
        return R.success(UvCache.getSevenDaysUv());
    }
    @GetMapping("/get/black")
    @Authentication("sys:statistics:black:get")
    public R<PageResultVO<BlackListVO>> getBlack(@RequestParam(value = "pageNum") Integer pageNum,
                                                 @RequestParam(value = "pageSize") Integer pageSize,
                                                 String key){
        if(pageSize > PageConstant.MAX_PAGE_SIZE){
            return R.error(BaseEnum.commonEnum.ERROR_PAGE_SIZE);
        }
        return blackListService.getBlackListPage(pageNum,pageSize,key);

    }
    @PostMapping("/add/black")
    @Authentication("sys:statistics:black:add")
    public R<Void> addBlack(@RequestBody @Validated(SaveGroup.class) BlackDTO dto){
        return blackListService.addBlack(dto);

    }
    @PostMapping("/remove/black")
    @Authentication("sys:statistics:black:remove")
    public R<Void> delBlack(@RequestBody @Validated(RemoveGroup.class) BlackDTO dto) {
        return blackListService.removeBlack(dto);

    }
    @GetMapping("/get/blackSort")
    public R<TreeMap<Long, String>> getBlackSort(){
        return R.success(BlackMenuCache.getBlackSortMap());
    }
}
